const axios = require('axios')

module.exports = function (api) {
  // 个人站点暂无数据
  const baseName = 'GitHub-Laziji'
  let title = ''
  
  api.loadSource(async ({ addCollection }) => {
    const newdata = addCollection('Newlist')
    const social = addCollection('Social')
    const blog = addCollection('Blog')
    const project = addCollection('Project')

    const data1 = axios.get(`https://api.github.com/users/${baseName}/gists?page=1&per_page=1`)
    const data2 = axios.get(`https://api.github.com/users/${baseName}/followers?page=1&per_page=9`)
    const data3 = axios.get(`https://api.github.com/users/${baseName}/gists?page=1&per_page=9`)
    const data4 = axios.get(`https://api.github.com/users/${baseName}/repos?page=1&per_page=9`)
    const [{ data: newsList }, { data: socialData }, { data: blogData }, { data: projectData }] = await Promise.all([data1, data2, data3, data4])
    
    if (newsList.length > 0) {
      title = Object.keys(newsList[0].files)[0]
    }else{
      title = '暂无数据'
    }
    newdata.addNode({
      title,
      createTime: newsList[0].createTime,
      updateTime: newsList[0].updateTime,
      description: newsList[0].description,
      content: newsList[0].content,
      id: newsList[0].id
    })
    for (const item of socialData) {
      social.addNode({
        name: item.login,
        avatarUrl: item.avatar_url,
        html_url: item.html_url
      })
    }
    for (const item of blogData) {
      const title = Object.keys(item.files)[0]
      blog.addNode({
        title,
        created_at: item.created_at,
        description: item.description,
        id: item.id,
      })
    }
    for (const item of projectData) {
      project.addNode({
        name: item.name,
        url: item.url,
        updateTime: item.updateTime,
        created_at: item.created_at,
        description: item.description,
        stargazersCount: item.stargazersCount,
        watchersCount: item.watchersCount,
        forksCount: item.forksCount,
        license: item.license,
        language: item.language
      })
    }
  })
}
